#include "ac_control.h"
#include "myfont.h"
#include "smart_home_ui.h"
#include "light_control.h"
#include "mqtt_cloud.h"
#include <stdio.h>
#include <time.h>
#include <string.h>

// 外部函数声明
extern void save_user_settings(void);

// 声明强制清屏函数

extern bool global_ac_power_state; // 声明外部全局变量


// 获取空调电源状态
bool get_ac_power_state(void) {
    return global_ac_power_state;
}
extern void force_clear_screen_ghosting(void);

// 界面元素
static lv_obj_t *ac_screen = NULL;
static lv_obj_t *label_title = NULL;
static lv_obj_t *btn_power = NULL;
static lv_obj_t *label_power = NULL;
static lv_obj_t *temp_value_label = NULL;
static lv_obj_t *temp_arc = NULL;
static lv_obj_t *label_mode_group = NULL;
static lv_obj_t *label_fan_group = NULL;
static lv_obj_t *btn_back = NULL;
static lv_obj_t *ac_time_label = NULL;
static lv_obj_t *ac_date_label = NULL;
static lv_timer_t *ac_timer_update = NULL;

// 模式与风速按钮组
static lv_obj_t *mode_btns[5];
static const char *mode_names[5] = {"制冷", "制热", "除湿", "送风", "自动"};
static int mode_index = 0; // 默认制冷

static lv_obj_t *fan_btns[4];
static const char *fan_names[4] = {"自动", "低", "中", "高"};
static int fan_index = 0; // 默认自动

static int current_temp = 24; // 16~30

// 获取空调当前温度
int get_ac_current_temp(void) {
    return current_temp;
}

// 获取空调当前模式
int get_ac_current_mode(void) {
    return mode_index;
}

// 获取空调当前风速
int get_ac_current_fan_speed(void) {
    return fan_index;
}

// 从设置恢复空调温度
void restore_ac_temperature(int temperature) {
    if (temperature >= 16 && temperature <= 30) {
        current_temp = temperature;
        printf("空调温度已恢复到: %d°C\n", temperature);
    } else {
        printf("警告: 无效的空调温度值 %d，使用默认值 24°C\n", temperature);
        current_temp = 24;
    }
}

// 从设置恢复空调模式
void restore_ac_mode(int mode) {
    if (mode >= 0 && mode <= 4) {
        mode_index = mode;
        printf("空调模式已恢复到: %d (%s)\n", mode, mode_names[mode]);
    } else {
        printf("警告: 无效的空调模式值 %d，使用默认模式 0 (制冷)\n", mode);
        mode_index = 0;
    }
}

// 从设置恢复空调风速
void restore_ac_fan_speed(int fan_speed) {
    if (fan_speed >= 0 && fan_speed <= 3) {
        fan_index = fan_speed;
        printf("空调风速已恢复到: %d (%s)\n", fan_speed, fan_names[fan_speed]);
    } else {
        printf("警告: 无效的空调风速值 %d，使用默认风速 0 (自动)\n", fan_speed);
        fan_index = 0;
    }
}

// 模式颜色数组
static lv_color_t mode_arc_colors[5] = {
    { .ch = {0xF7, 0xC3, 0x4F, 0x00} }, /* 0x4FC3F7 冷蓝 */
    { .ch = {0x22, 0x7E, 0xE6, 0x00} }, /* 0xE67E22 暖橙 */
    { .ch = {0xD4, 0xBC, 0x00, 0x00} }, /* 0x00BCD4 青 */
    { .ch = {0x9A, 0xA6, 0x26, 0x00} }, /* 0x26A69A 绿青 */
    { .ch = {0xDB, 0x98, 0x34, 0x00} }, /* 0x3498DB 蓝 */
};

// 刷新空调界面UI元素（用于云平台命令后的界面同步）
void refresh_ac_control_ui(void) {
    // 检查空调界面是否处于活动状态
    if (!ac_screen || !lv_obj_is_valid(ac_screen)) {
        return; // 空调界面不活动，无需刷新
    }
    
    lv_obj_t *current_screen = lv_scr_act();
    if (current_screen != ac_screen) {
        return; // 当前不在空调界面，无需刷新
    }
    
    printf("刷新空调界面UI...\n");
    
    // 1. 刷新电源按钮状态
    if (btn_power) {
        bool ac_on = get_ac_power_state();
        lv_color_t bg = ac_on ? lv_color_hex(0x27AE60) /* 绿 */ : lv_color_hex(0x7F8C8D) /* 灰 */;
        lv_obj_set_style_bg_color(btn_power, bg, 0);
        lv_obj_set_style_bg_opa(btn_power, LV_OPA_COVER, 0);
    }
    
    // 2. 刷新温度圆环和标签
    if (temp_arc) {
        lv_arc_set_value(temp_arc, current_temp);
    }
    if (temp_value_label) {
        char buf[16];
        snprintf(buf, sizeof(buf), "%d°C", current_temp);
        lv_label_set_text(temp_value_label, buf);
    }
    
    // 3. 刷新模式按钮状态
    for (int i = 0; i < 5; i++) {
        if (mode_btns[i]) {
            bool checked = (i == mode_index);
            if (checked) {
                lv_obj_set_style_bg_color(mode_btns[i], lv_color_hex(0x2980b9), 0);
            } else {
                lv_obj_set_style_bg_color(mode_btns[i], lv_color_hex(0x34495e), 0);
            }
        }
    }
    
    // 4. 刷新风速按钮状态
    for (int i = 0; i < 4; i++) {
        if (fan_btns[i]) {
            bool checked = (i == fan_index);
            if (checked) {
                lv_obj_set_style_bg_color(fan_btns[i], lv_color_hex(0x2980b9), 0);
            } else {
                lv_obj_set_style_bg_color(fan_btns[i], lv_color_hex(0x34495e), 0);
            }
        }
    }
    
    // 5. 刷新圆环颜色（根据模式和电源状态）
    if (temp_arc) {
        bool ac_on = get_ac_power_state();
        lv_color_t arc_col = ac_on ? mode_arc_colors[mode_index] : lv_color_hex(0x7F8C8D);
        lv_obj_set_style_arc_color(temp_arc, arc_col, LV_PART_INDICATOR);
    }
    
    printf("空调界面UI刷新完成\n");
}

// 移除本地的 ac_power_on，使用全局状态


static void apply_temp_label(void) {
    char buf[16];
    if(temp_value_label) {
        snprintf(buf, sizeof(buf), "%d°C", current_temp);
        lv_label_set_text(temp_value_label, buf);
    }
}

// 空调界面时间更新回调函数
static void ac_update_time_task(lv_timer_t *timer) {
    static time_t last_update_s = 0;
    static char last_date_str[32] = "";
    static char last_time_str[32] = "";
    
    time_t now;
    time(&now);
    
    // 当timer为NULL时（手动调用），强制更新一次
    bool force_update = (timer == NULL);
    
    // 仅在秒有变化时更新，避免重复重绘
    if(!force_update && now == last_update_s) return;
    last_update_s = now;
    if(ac_date_label == NULL || ac_time_label == NULL) return; // 确保标签已创建
    
    time_t raw_time;
    struct tm *time_info;
    char date_str[32];
    char time_str[32];
    
    raw_time = now;
    time_info = localtime(&raw_time);
    
    // 分别格式化日期和时间
    strftime(date_str, sizeof(date_str), "%Y-%m-%d", time_info);
    strftime(time_str, sizeof(time_str), "%H:%M:%S", time_info);
    
    // 设置日期和时间标签文本
    if(force_update || strcmp(last_date_str, date_str) != 0) {
        lv_label_set_text(ac_date_label, date_str);
        strcpy(last_date_str, date_str);
    }
    if(force_update || strcmp(last_time_str, time_str) != 0) {
        lv_label_set_text(ac_time_label, time_str);
        strcpy(last_time_str, time_str);
    }
}

static void update_theme_by_mode(void);

static void power_event_cb(lv_event_t *e) {
    if(lv_event_get_code(e) != LV_EVENT_CLICKED) return;

    // 切换全局空调状态
    bool new_state = !get_ac_power_state();
    sync_ac_power_state(new_state);

    // 上传空调数据
    mqtt_upload_ac_data(new_state, current_temp, mode_index, fan_index);
    
    // 保存用户设置
    save_user_settings();

    // 根据电源状态调整按钮颜色
    if(btn_power) {
        lv_color_t bg = new_state ? lv_color_hex(0x27AE60) /* 绿 */ : lv_color_hex(0x7F8C8D) /* 灰 */;
        lv_obj_set_style_bg_color(btn_power, bg, 0);
        lv_obj_set_style_bg_opa(btn_power, LV_OPA_COVER, 0);
    }
    // 同步圆环颜色
    update_theme_by_mode();
}

static void temp_arc_event_cb(lv_event_t *e) {
    lv_obj_t *arc = lv_event_get_target(e);
    if(lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED) {
        int v = lv_arc_get_value(arc);
        if(v < 16) v = 16;
        if(v > 30) v = 30;
        if(v != current_temp) {
            current_temp = v;
            apply_temp_label();

            // 如果空调已开启，上传温度变化
            if(get_ac_power_state()) {
                mqtt_upload_ac_data(true, current_temp, mode_index, fan_index);
            }
            
            // 保存用户设置
            save_user_settings();
        }
    }
}

static void set_btn_checked_style(lv_obj_t *btn, bool checked) {
    // 默认与选中样式（简单高亮）
    if(checked) {
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x2980b9), 0);
    } else {
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x34495e), 0);
    }
    lv_obj_set_style_radius(btn, 8, 0);
    lv_obj_set_style_border_width(btn, 0, 0);
}

static void update_theme_by_mode(void) {
    // 关机时圆环为灰色，开机按模式着色
    bool ac_on = get_ac_power_state();
    lv_color_t arc_col = ac_on ? mode_arc_colors[mode_index] : lv_color_hex(0x7F8C8D);
    if(temp_arc) {
        lv_obj_set_style_arc_color(temp_arc, arc_col, LV_PART_INDICATOR);
    }
}

static void back_btn_event_cb(lv_event_t *e) {
    if(lv_event_get_code(e) != LV_EVENT_CLICKED) return;
    
    // 使用安全的返回方式，避免在事件回调中直接删除当前屏幕
    return_to_smart_home_ui();
}

// 延迟切换到灯光界面的回调函数
static void delayed_light_switch_cb(lv_timer_t *timer) {
    // 清理AC界面定时器
    if(ac_timer_update) {
        lv_timer_del(ac_timer_update);
        ac_timer_update = NULL;
    }
    
    force_clear_screen_ghosting();
    create_light_control_screen();
    lv_timer_del(timer);  // 删除定时器
}

// 滑动事件处理函数
static void swipe_event_cb(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_GESTURE) {
        lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
        if(dir == LV_DIR_LEFT) {
            // 向左滑动，延迟切换到灯光控制界面
            lv_timer_create(delayed_light_switch_cb, 5, NULL);
        }
    }
}

// 灯光按钮回调函数
static void lights_btn_event_cb(lv_event_t *e) {
    if(lv_event_get_code(e) != LV_EVENT_CLICKED) return;
    
    // 延迟切换到灯光控制界面
    lv_timer_create(delayed_light_switch_cb, 5, NULL);
}

static void mode_btn_event_cb(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    if(lv_event_get_code(e) != LV_EVENT_CLICKED) return;
    for(int i = 0; i < 5; i++) {
        if(btn == mode_btns[i]) {
            mode_index = i;
            // 独占选中
            for(int j = 0; j < 5; j++) set_btn_checked_style(mode_btns[j], j == i);
            update_theme_by_mode();

            // 如果空调已开启，上传模式变化
            if(get_ac_power_state()) {
                mqtt_upload_ac_data(true, current_temp, mode_index, fan_index);
            }
            
            // 保存用户设置
            save_user_settings();
            break;
        }
    }
}

static void fan_btn_event_cb(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    if(lv_event_get_code(e) != LV_EVENT_CLICKED) return;
    for(int i = 0; i < 4; i++) {
        if(btn == fan_btns[i]) {
            fan_index = i;
            for(int j = 0; j < 4; j++) set_btn_checked_style(fan_btns[j], j == i);
            
            // 如果空调已开启，上传风速变化
            if(get_ac_power_state()) {
                mqtt_upload_ac_data(true, current_temp, mode_index, fan_index);
            }
            
            // 保存用户设置
            save_user_settings();
            break;
        }
    }
}

void create_ac_control_screen(void) {
    // 清理旧的对象引用
    ac_screen = NULL;
    label_title = NULL;
    btn_power = NULL;
    label_power = NULL;
    temp_value_label = NULL;
    temp_arc = NULL;
    label_mode_group = NULL;
    label_fan_group = NULL;
    btn_back = NULL;
    ac_time_label = NULL;
    ac_date_label = NULL;
    if(ac_timer_update) {
        lv_timer_del(ac_timer_update);
        ac_timer_update = NULL;
    }
    for(int i = 0; i < 5; i++) mode_btns[i] = NULL;
    for(int i = 0; i < 4; i++) fan_btns[i] = NULL;
    

    
    // 创建新的空调屏幕
    ac_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(ac_screen, lv_color_hex(0x2c3e50), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(ac_screen, LV_OPA_COVER, LV_PART_MAIN);  // 确保背景完全不透明
    lv_obj_clear_flag(ac_screen, LV_OBJ_FLAG_SCROLLABLE);
    
    // 添加滑动手势支持
    lv_obj_add_event_cb(ac_screen, swipe_event_cb, LV_EVENT_GESTURE, NULL);

    // HVAC图标
    lv_obj_t *hvac_title_icon = lv_img_create(ac_screen);
    lv_img_set_src(hvac_title_icon, &hvac);
    lv_obj_set_size(hvac_title_icon, 40, 30);  // 保持原始比例
    lv_obj_align(hvac_title_icon, LV_ALIGN_TOP_MID, -60, 8);
    
    // 标题
    label_title = lv_label_create(ac_screen);
    lv_obj_set_style_text_font(label_title, &lv_font_source_han_sans_bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(label_title, lv_color_hex(0xecf0f1), LV_PART_MAIN);
    lv_label_set_text(label_title, "空调控制");
    lv_obj_align(label_title, LV_ALIGN_TOP_MID, 10, 6);

    // 返回按钮（左上角）
    btn_back = lv_btn_create(ac_screen);
    lv_obj_set_size(btn_back, 72, 40);
    lv_obj_align(btn_back, LV_ALIGN_TOP_LEFT, 12, 10);
    set_btn_checked_style(btn_back, false);
    lv_obj_t *lbl_back = lv_label_create(btn_back);
    lv_obj_set_style_text_font(lbl_back, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(lbl_back, lv_color_hex(0xFFFFFF), 0);
    lv_label_set_text(lbl_back, LV_SYMBOL_LEFT " 返回");
    lv_obj_center(lbl_back);
    lv_obj_add_event_cb(btn_back, back_btn_event_cb, LV_EVENT_ALL, NULL);

    // 添加时间显示（右上角）
    // 日期时间标签（右上角，贴着屏幕右侧）
    ac_date_label = lv_label_create(ac_screen);
    lv_obj_set_style_text_font(ac_date_label, &lv_font_source_han_sans_bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(ac_date_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_label_set_text(ac_date_label, "加载中...");  // 临时文本，立即会被更新
    lv_obj_align(ac_date_label, LV_ALIGN_TOP_RIGHT, -5, 10);  // 与其他页面一致，贴近右侧

    ac_time_label = lv_label_create(ac_screen);
    lv_obj_set_style_text_font(ac_time_label, &lv_font_source_han_sans_bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(ac_time_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_label_set_text(ac_time_label, "加载中...");  // 临时文本，立即会被更新
    lv_obj_align(ac_time_label, LV_ALIGN_TOP_RIGHT, -5, 40);  // 使用绝对定位，确保右侧完全对齐

    // 添加灯光页面切换按钮（右上角下方）
    lv_obj_t *btn_lights = lv_btn_create(ac_screen);
    lv_obj_set_size(btn_lights, 72, 40);
    lv_obj_align_to(btn_lights, ac_time_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_style_bg_color(btn_lights, lv_color_hex(0x34495e), 0);
    lv_obj_set_style_radius(btn_lights, 8, 0);
    lv_obj_set_style_border_width(btn_lights, 0, 0);
    lv_obj_t *lbl_lights = lv_label_create(btn_lights);
    lv_obj_set_style_text_font(lbl_lights, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(lbl_lights, lv_color_hex(0xFFFFFF), 0);
    lv_label_set_text(lbl_lights, "灯光 " LV_SYMBOL_RIGHT);
    lv_obj_center(lbl_lights);
    lv_obj_add_event_cb(btn_lights, lights_btn_event_cb, LV_EVENT_ALL, NULL);

    // 圆形温度滑块（lv_arc）
    temp_arc = lv_arc_create(ac_screen);
    // 适当上移温度滑块，给下方按钮留更多空间
    lv_obj_set_size(temp_arc, 300, 300);
    lv_obj_align(temp_arc, LV_ALIGN_CENTER, 0, -60);
    lv_arc_set_bg_angles(temp_arc, 135, 45);
    lv_arc_set_range(temp_arc, 16, 30);
    lv_arc_set_value(temp_arc, current_temp);
    lv_obj_set_style_arc_width(temp_arc, 18, LV_PART_INDICATOR);
    lv_obj_set_style_arc_color(temp_arc, lv_color_hex(0x4FC3F7), LV_PART_INDICATOR);
    lv_obj_set_style_arc_width(temp_arc, 18, LV_PART_MAIN);
    lv_obj_set_style_arc_color(temp_arc, lv_color_hex(0x22313F), LV_PART_MAIN);
    lv_obj_add_event_cb(temp_arc, temp_arc_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    // 温度显示（位于圆形中央）
    temp_value_label = lv_label_create(ac_screen);
    // 使用支持“°”的字体（温度中央显示）
    lv_obj_set_style_text_font(temp_value_label, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(temp_value_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    apply_temp_label();
    lv_obj_align(temp_value_label, LV_ALIGN_CENTER, 0, -60);

    // 模式按钮组标题
    label_mode_group = lv_label_create(ac_screen);
    lv_obj_set_style_text_font(label_mode_group, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(label_mode_group, lv_color_hex(0xFFFFFF), 0);
    lv_label_set_text(label_mode_group, "模式");
    // 对齐：先放置到靠近按钮位置，随后精确对齐到按钮左上
    lv_obj_align(label_mode_group, LV_ALIGN_CENTER, -360, 136);

    // 模式按钮组
    for(int i = 0; i < 5; i++) {
        mode_btns[i] = lv_btn_create(ac_screen);
        lv_obj_set_size(mode_btns[i], 110, 48);
        lv_obj_align(mode_btns[i], LV_ALIGN_CENTER, -220 + i * 110, 160);
        set_btn_checked_style(mode_btns[i], i == mode_index);
        lv_obj_add_event_cb(mode_btns[i], mode_btn_event_cb, LV_EVENT_CLICKED, NULL);
        // 单标签：图标 + 文本，整体居中
        const char *mode_icon = "";
        switch(i){
            case 0: mode_icon = "❄"; break;      // 制冷
            case 1: mode_icon = "☀"; break;      // 制热
            case 2: mode_icon = "💧"; break;      // 除湿
            case 3: mode_icon = LV_SYMBOL_REFRESH; break; // 送风
            default: mode_icon = LV_SYMBOL_SETTINGS; break; // 自动
        }
        lv_obj_t *lbl = lv_label_create(mode_btns[i]);
        lv_obj_set_style_text_font(lbl, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_set_style_text_color(lbl, lv_color_hex(0xFFFFFF), 0);
        char buf[32];
        snprintf(buf, sizeof(buf), "%s %s", mode_icon, mode_names[i]);
        lv_label_set_text(lbl, buf);
        lv_obj_center(lbl);
    }
    // 精确对齐“模式”标题到首个按钮左上，略上移 4 像素
    lv_obj_align_to(label_mode_group, mode_btns[0], LV_ALIGN_OUT_TOP_LEFT, 0, -4);

    // 风速按钮组标题
    label_fan_group = lv_label_create(ac_screen);
    lv_obj_set_style_text_font(label_fan_group, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(label_fan_group, lv_color_hex(0xFFFFFF), 0);
    lv_label_set_text(label_fan_group, "风速");
    // 先放置到靠近按钮的位置，随后精确对齐到按钮左上
    lv_obj_align(label_fan_group, LV_ALIGN_CENTER, -360, 226);

    // 风速按钮组
    for(int i = 0; i < 4; i++) {
        fan_btns[i] = lv_btn_create(ac_screen);
        lv_obj_set_size(fan_btns[i], 110, 48);
        lv_obj_align(fan_btns[i], LV_ALIGN_CENTER, -165 + i * 110, 250);
        set_btn_checked_style(fan_btns[i], i == fan_index);
        lv_obj_add_event_cb(fan_btns[i], fan_btn_event_cb, LV_EVENT_CLICKED, NULL);
        // 单标签：图标 + 文本，整体居中
        lv_obj_t *lbl = lv_label_create(fan_btns[i]);
        lv_obj_set_style_text_font(lbl, &lv_font_source_han_sans_bold_20, 0);
        lv_obj_set_style_text_color(lbl, lv_color_hex(0xFFFFFF), 0);
        char fbuf[24];
        snprintf(fbuf, sizeof(fbuf), "%s %s", LV_SYMBOL_REFRESH, fan_names[i]);
        lv_label_set_text(lbl, fbuf);
        lv_obj_center(lbl);
    }
    // 精确对齐“风速”标题到第一枚风速按钮左上，略上移 4 像素
    lv_obj_align_to(label_fan_group, fan_btns[0], LV_ALIGN_OUT_TOP_LEFT, 0, -4);

    // 去除扫风控制（按需求移除）

    // 电源键（置于圆环内部温度文字下方，不遮挡风速按钮）
    btn_power = lv_btn_create(ac_screen);
    lv_obj_set_size(btn_power, 72, 72);
    lv_obj_set_style_radius(btn_power, LV_RADIUS_CIRCLE, 0);
    // 将按钮再次下移，避免遮挡温度文字（落在圆环下半部）
    lv_obj_align(btn_power, LV_ALIGN_CENTER, 0, 0);
    // 初始未开启状态样式
    lv_obj_set_style_bg_color(btn_power, lv_color_hex(0x7F8C8D), 0);
    lv_obj_set_style_bg_opa(btn_power, LV_OPA_COVER, 0);
    lv_obj_set_style_shadow_width(btn_power, 0, 0);
    lv_obj_add_event_cb(btn_power, power_event_cb, LV_EVENT_CLICKED, NULL);
    label_power = lv_label_create(btn_power);
    // 使用内置 Montserrat 字体以正确显示 LV_SYMBOL_POWER
    lv_obj_set_style_text_font(label_power, &lv_font_montserrat_20, 0);
    lv_obj_set_style_text_color(label_power, lv_color_hex(0xFFFFFF), 0);
    lv_label_set_text(label_power, LV_SYMBOL_POWER);
    lv_obj_center(label_power);

    // 根据默认模式应用主题
    update_theme_by_mode();
    
    // 同步全局空调状态到界面
    bool ac_on = get_ac_power_state();
    if(btn_power) {
        lv_color_t bg = ac_on ? lv_color_hex(0x27AE60) : lv_color_hex(0x7F8C8D);
        lv_obj_set_style_bg_color(btn_power, bg, 0);
        lv_obj_set_style_bg_opa(btn_power, LV_OPA_COVER, 0);
    }
    
    // 创建时间更新定时器
    ac_timer_update = lv_timer_create(ac_update_time_task, 1000, NULL);
    // 立即更新一次时间
    ac_update_time_task(NULL);
    
    // 使用安全的屏幕切换方式，避免段错误
    // 使用快速淡入动画，提供更流畅的切换体验
    lv_scr_load_anim(ac_screen, LV_SCR_LOAD_ANIM_FADE_ON, 100, 0, false);
    
    // 让LVGL自动处理旧屏幕清理，避免手动删除导致的内存问题
}

void clear_and_load_ac(void) {
    // 简化：直接进入空调界面，避免双重 lv_scr_load 造成的空白
    create_ac_control_screen();
}


